// Tencent is pleased to support the open source community by making
// 蓝鲸智云 - 监控平台/日志平台 (BlueKing - Monitor/Log) available.
// Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved.
// Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://opensource.org/licenses/MIT
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//

package main

import (
	"fmt"
	"os"

	"github.com/TencentBlueKing/collector-go-sdk/v2/bkbeat/beat"
	"github.com/elastic/beats/libbeat/cmd/instance"
	"github.com/elastic/beats/libbeat/publisher/processing"

	"github.com/TencentBlueKing/bk-collector/confengine"
	"github.com/TencentBlueKing/bk-collector/controller"
)

var (
	appName = "bk-collector"
	version = "unknown"
)

func main() {
	settings := instance.Settings{Processing: processing.MakeDefaultSupport(false)}
	pubConfig := beat.PublishConfig{PublishMode: beat.GuaranteedSend}

	config, err := beat.InitWithPublishConfig(appName, version, pubConfig, settings)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to init beat config: %v", err)
		os.Exit(1)
	}

	collector, err := controller.New(confengine.New(config))
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to create new beat: %v", err)
		os.Exit(1)
	}

	if err := collector.Start(); err != nil {
		fmt.Fprintf(os.Stderr, "failed to start collector %v", err)
		os.Exit(1)
	}

	for {
		select {
		case <-beat.ReloadChan:
			// 重载信号
			if conf := beat.GetConfig(); conf != nil {
				if err := collector.Reload(confengine.New(conf)); err != nil {
					fmt.Fprint(os.Stderr, "failed to reload controller")
				}
			}

		case <-beat.Done:
			// 结束信号
			if err := collector.Stop(); err != nil {
				fmt.Fprint(os.Stderr, "failed to stop controller")
			}
			return
		}
	}
}
